package com.example.module.custom.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 初始RecyclerView的adapter父类
 *
 * @package com.je.adapter
 * @anthor taoyong
 * @time 2017/3/30
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter {
    private static final String TAG = "BaseAdapter";
    protected Context context;
    protected List<T> tlist;//集合
    protected int mLayout;//主体布局
    protected int mHeaderLayout;//首布局
    protected int mFooterLayout;//尾布局
    /**
     * identification >>> 标识
     * NOLAYOUT >>> 没有布局
     * FIRSTLAYOUT >>> 加载首布局
     * TAILLAYOUT >>> 加载尾布局
     * THELAYOUT >>> 加载首尾布局
     */
    private int identification;
    /**
     * 没有布局
     */
    public static final int NOLAYOUT = 0;
    /**
     * 加载首布局
     */
    public static final int FIRSTLAYOUT = 1;
    /**
     * 加载尾布局
     */
    public static final int TAILLAYOUT = 2;
    /**
     * 加载首尾布局
     */
    public static final int THELAYOUT = 3;

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public BaseAdapter(Context context, List<T> tlist, int mLayout) {
        identification = NOLAYOUT;
        this.context = context;
        this.tlist = tlist;
        this.mLayout = mLayout;
    }

    /**
     * 添加头部View
     */
    public void addHeaderView(int mHeaderLayout) {
        this.mHeaderLayout = mHeaderLayout;
        identification = FIRSTLAYOUT;
    }

    /**
     * 添加底部View
     */
    public void addFooterView(int mFooterLayout) {
        this.mFooterLayout = mFooterLayout;
        identification = TAILLAYOUT;
    }

    /**
     * 添加头部与底部View
     */
    public void addHeaderAndFooterView(int mHeaderLayout, int mFooterLayout) {
        this.mHeaderLayout = mHeaderLayout;
        this.mFooterLayout = mFooterLayout;
        identification = THELAYOUT;
    }

    /**
     * 清除 自己添加的 首尾布局
     */
    public void cleaMoreView() {
        identification = NOLAYOUT;
    }

    /**
     * 取出布局标识
     *
     * @return NOLAYOUT >>> 没有布局 0
     * FIRSTLAYOUT >>> 加载首布局 1
     * TAILLAYOUT >>> 加载尾布局 0
     * THELAYOUT >>> 加载首尾布局 1
     */
    public int getIdentification() {
        return identification;
    }


    /**
     * 设置布局标识
     *
     * @param identification >>> 没有布局 0
     *                       FIRSTLAYOUT >>> 加载首布局 1
     *                       TAILLAYOUT >>> 加载尾布局 0
     *                       THELAYOUT >>> 加载首尾布局 1
     */
    public void setIdentification(int identification) {
        this.identification = identification;
    }

    /**
     * 普通item
     */
    public static final int ITEMTYPE = 0;
    /**
     * 头部item
     */
    public static final int HEADERTYPE = 1;
    /**
     * 尾部item
     */
    public static final int FOOTERTYPE = 2;

    @Override
    public int getItemViewType(int position) {
        if (identification == FIRSTLAYOUT) {
            if (position == 0) {
                return HEADERTYPE;
            } else {
                return ITEMTYPE;
            }
        } else if (identification == TAILLAYOUT) {
            if (position + 1 == getItemCount()) {
                return FOOTERTYPE;
            } else {
                return ITEMTYPE;
            }
        } else if (identification == THELAYOUT) {
            if (position == 0) {
                return HEADERTYPE;
            } else if (position + 1 == getItemCount()) {
                return FOOTERTYPE;
            } else {
                return ITEMTYPE;
            }
        }
        return ITEMTYPE;
    }

    @Override
    public int getItemCount() {
        int count = tlist == null ? 0 : tlist.size();
        if (identification == FIRSTLAYOUT || identification == TAILLAYOUT) {
            count += 1;
        } else if (identification == THELAYOUT) {
            count += 2;
        }
        return count;
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADERTYPE) {
            View view = LayoutInflater.from(context).inflate(mHeaderLayout, parent, false);
            //HeaderViewHolder holder = new HeaderViewHolder(view);
            return getHeaderViewHolder(view);
        } else if (viewType == ITEMTYPE) {
            View view = LayoutInflater.from(context).inflate(mLayout, parent, false);
            //ItemViewHolder holder = new ItemViewHolder(view);
            return getItemViewHolder(view);
        } else if (viewType == FOOTERTYPE) {
            View view = LayoutInflater.from(context).inflate(mFooterLayout, parent, false);
            //FooterViewHolder holder = new FooterViewHolder(view);
            return getFooterViewHolder(view);
        }
        return null;
    }

    /**
     * 得到顶部ViewHolder
     */
    protected abstract RecyclerView.ViewHolder getHeaderViewHolder(View view);

    /**
     * 得到普通ViewHolder
     */
    protected abstract RecyclerView.ViewHolder getItemViewHolder(View view);

    /**
     * 得到底部ViewHolder
     */
    protected abstract RecyclerView.ViewHolder getFooterViewHolder(View view);

    public abstract void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position);

    /*public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof HeaderViewHolder) {
            HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
            operationHeaderView(holder, position);
        } else if (viewHolder instanceof ItemViewHolder) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            operationItemView(holder, position);
        } else if (viewHolder instanceof FooterViewHolder) {
            FooterViewHolder holder = (FooterViewHolder) viewHolder;
            operationFooterView(holder, position);
        }
    }*/

    //protected void operationHeaderView(HeaderViewHolder holder, int position) { }

    //protected void operationItemView(ItemViewHolder holder, int position) { }

    //protected void operationFooterView(FooterViewHolder holder, int position) { }

    /**
     * 实现ViewHolder的抽象方法
     */
    protected class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public <X extends View> X findTById(int resId) {
            return (X) itemView.findViewById(resId);
        }

        /**
         * 提示框
         *
         * @param value
         */
        protected void toast_MakeText(String value) {
            Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 暂无数据
     */
    public static final String NODATA = "NODATA";
    /**
     * 上拉加载
     */
    public static final String TOPPULLLOADING = "TOPPULLLOADING";
    /**
     * 正在加载
     */
    public static final String BEINGLOADED = "BEINGLOADED";
    /**
     * 加载状态
     */
    private String LOADINGSTATE = TOPPULLLOADING;

    /**
     * 取出加载标识
     *
     * @return NODATA >>> 暂无数据
     * TOPPULLLOADING >>> 上拉加载
     * BEINGLOADED >>> 正在加载
     */
    public String getLOADINGSTATE() {
        return LOADINGSTATE;
    }

    /**
     * 设置加载标识
     *
     * @param LOADINGSTATE NODATA >>> 暂无数据
     *                     TOPPULLLOADING >>> 上拉加载
     *                     BEINGLOADED >>> 正在加载
     */
    public void setLOADINGSTATE(String LOADINGSTATE) {
        this.LOADINGSTATE = LOADINGSTATE;
        notifyDataSetChanged();
    }

    protected OnItemClickListener onItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void OnItemClick(int postion, View view);
    }
}